package com.jinxin.platform.cdperprotypay.service.impl;

import com.jinxin.platform.cdperprotypay.mapper.ChargeStandardMapper;
import com.jinxin.platform.cdperprotypay.mapper.ChargeItemsMapper;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysUser;
import com.jinxin.platform.cdperprotypay.pojo.domain.ChargeItems;
import com.jinxin.platform.cdperprotypay.pojo.domain.ChargeStandard;
import com.jinxin.platform.cdperprotypay.service.ChargeItemsService;
import com.jinxin.platform.cdperprotypay.support.ModularSupport;
import org.springframework.stereotype.Service;
import com.jinxin.platform.base.common.pojo.JsonResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 收费项目(ChargeItems)表服务实现类
 *
 * @author zhangpeng
 * @since 2020-06-28 17:31:04
 */
@Service("chargeItemsService")
public class ChargeItemsServiceImpl implements ChargeItemsService {
    @Resource
    private ChargeItemsMapper chargeItemsMapper;

    @Resource
    private ChargeStandardMapper chargeStandardMapper;

    @Resource
    private ModularSupport modularSupport;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonResult queryById(String id) {
        ChargeItems chargeItems = this.chargeItemsMapper.queryById(id);
        return JsonResult.ok(chargeItems);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param chargeItems 实例对象
     * @return 对象列表
     */
    @Override
    public JsonResult queryAll(ChargeItems chargeItems) {
        List<ChargeItems> chargeItemsList = this.chargeItemsMapper.queryAll(chargeItems);
        return JsonResult.ok(chargeItemsList);
    }

    /**
     * 新增数据
     *
     * @param chargeItems 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult insert(ChargeItems chargeItems) {
        SysUser user = modularSupport.getCurrentUser();
        String userName = "未知";
        if (user != null) {
            userName = user.getUserName();
        }
        chargeItems.setCreater(userName);
        chargeItems.setCreateTime(new Date());
        int i = this.chargeItemsMapper.insert(chargeItems);
        return i > 0 ? JsonResult.ok("新增成功", chargeItems) : JsonResult.error("新增失败！");
    }

    /**
     * 修改数据
     *
     * @param chargeItems 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult update(ChargeItems chargeItems) {
        int i = this.chargeItemsMapper.update(chargeItems);
        return i > 0 ? JsonResult.ok("修改成功！", chargeItems) : JsonResult.error("修改失败！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public JsonResult deleteById(String id) {
        ChargeStandard chargeStandard = new ChargeStandard();
        chargeStandard.setStandardStatus("1"); //判断是否启用
        chargeStandard.setItemsId(id);
        List<ChargeStandard> standardList = chargeStandardMapper.queryAll(chargeStandard);
        if (standardList.size() > 0) {
            return JsonResult.error("该收费项目下有启用的收费标准，无法删除！");
        }
        //删除该项目下的收费标准
        chargeStandard.setStandardStatus("0");
        List<ChargeStandard> list = chargeStandardMapper.queryAll(chargeStandard);
        for (ChargeStandard c : list) {
            chargeStandardMapper.deleteById(c.getId());
        }
        //删除项目
        return this.chargeItemsMapper.deleteById(id) > 0 ? JsonResult.ok("删除成功！", null) : JsonResult.error("删除失败！");
    }
}